package idv.jieh.commons.util;

public class CodeParser {

  public static String decode(String arg_password) {
    String sPassword = "";
    int ibase, li_char;
    String ls_codestr;
    String ls_transpwd = "";
    char lc_char;

    try {
      if (DataAnalysis.isLetter(arg_password)) {
        return arg_password;
      }
      ibase = Integer.parseInt(arg_password.substring(0, 3));
      for (int i = 1; i <= (arg_password.length() - 1) / 3; i++) {

        ls_codestr = arg_password.substring((i) * 3, (i) * 3 + 3);

        ls_transpwd += (char) Integer.parseInt(ls_codestr);
      }

      lc_char = ls_transpwd.charAt(0);

      li_char = new Character(lc_char).compareTo(new Character('\0')) - ibase;
      sPassword += (char) li_char;
      for (int i = 1; i < ls_transpwd.length(); i++) {
        lc_char = ls_transpwd.charAt(i);
        if ((i % 2) == 0) {
          li_char = new Character(lc_char).compareTo(new Character('\0')) +
                    (ibase + i + 1);
        } else {
          li_char = new Character(lc_char).compareTo(new Character('\0')) -
                    (ibase + i + 1);
        }
        if (li_char > 255) {
          li_char -= 256;
        }
        if (li_char < 0) {
          li_char += 256;
        }
        sPassword += (char) li_char;
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return sPassword.trim();
  }

  public static String encode(String arg_pass) {
    String rtn = "";
    String ls_password = "";
    int li_base;
    int li_char;
    char lc_char;
    String ls_tmp;

    try {
      if (arg_pass == null || arg_pass.trim().equals("")) {
        return arg_pass;
      }
      li_base = getBase(arg_pass);
      if (li_base > 255) {
        li_base -= 256;
      }
      ls_password += (char) li_base;
      lc_char = arg_pass.charAt(0);
      li_char = (int) lc_char + li_base;
      if (li_char > 255) {
        li_char -= 256;
      }
      ls_password += (char) li_char;
      for (int i = 1; i < arg_pass.length(); i++) {
        lc_char = arg_pass.charAt(i);
        if (i % 2 == 1) {
          li_char = (int) lc_char + li_base + (i + 1);
        } else {
          li_char = (int) lc_char - (li_base + (i + 1));
        }
        if (li_char > 255) {
          li_char -= 256;
        }
        if (li_char < 0) {
          li_char += 256;
        }
        ls_password += (char) li_char;
      }
      for (int i = 0; i < ls_password.length(); i++) {
        li_char = ls_password.charAt(i);
        ls_tmp = "000" + new Integer((int) li_char).toString();
        rtn += ls_tmp.substring(ls_tmp.length() - 3, ls_tmp.length());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return rtn;
  }

  private static int getBase(String arg_pass) {
    int rtn = 0;
    int li_n;
    long ll_num = 0;

    li_n = arg_pass.trim().length();
    for (int i = 0; i < li_n; i++) {
      ll_num += ((int) arg_pass.charAt(i));
    }
    rtn = (int) (32 + ((int) (System.currentTimeMillis() % 10)) +
                 (ll_num / (li_n + 1)));
    return rtn;
  }

  ///*
    public static void main(String[] args){

      String a = null;
      if(a != null && a.equals("a")){
        System.out.println("yes");
      }

      System.out.println("encode : " + CodeParser.encode("!voice"));
      System.out.println("encode : " + CodeParser.encode("daspfetcsjava"));
      System.out.println("encode : " + CodeParser.encode("abc123!@#"));

      System.out.println("encode : " + CodeParser.encode("654321"));

      System.out.println("decode : " + CodeParser.decode("081130133223137223141"));

      System.out.println("decode : " + CodeParser.decode("079128131225135225139"));

    }//*/

} // End Class CodeParser
